<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;


class CommunityMember extends Model
{
    // 表名
    protected $name = 'community_member';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    use SoftDelete;
    protected $deleteTime = 'deletetime';
    
    // 追加属性
    protected $append = [
        'deletetime_text',
        'expiretime_text',
        'paid_text',
        'state_text'
    ];
    

    
    public function getPaidList()
    {
        return ['0' => __('Paid 0'),'1' => __('Paid 1')];
    }     

    public function getStateList()
    {
        return ['0' => __('State 0'),'1' => __('State 1')];
    }     


    public function getExpiretimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['expiretime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPaidTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['paid'];
        $list = $this->getPaidList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStateTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['state'];
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setExpiretimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function community()
    {
        return $this->belongsTo('User', 'community_id')->setEagerlyType(0);
    }


}
